<template>
  <div class="login-wrap">
    <div class="login_rsp">
      <div class="login_work">
        <div class="ms-title">戬威后台管理系统</div>
        <el-form
          :model="param"
          :rules="rules"
          ref="login"
          label-width="0px"
          class="ms-content"
        >
          <el-form-item prop="username">
            <el-input v-model="param.username" placeholder="username">
              <template #prepend>
                <el-button icon="el-icon-user"></el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" v-if="codes">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="param.password"
            >
              <template #prepend>
                <el-button icon="el-icon-lock"></el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="codebase" style="display: flex" v-else>
            <el-input
              placeholder="请输入验证码"
              style="width: 125px"
              v-model="param.codebase"
            >
            </el-input>
            <el-button
              type="success"
              v-show="show"
              @click="getCode"
              plain
              style=""
              >发送验证码</el-button
            >
            <el-button
              style="width: 105px"
              type="success"
              v-show="!show"
              plain
              disabled
              >{{ count }} s</el-button
            >
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm()">登录</el-button>
          </div>
          <!-- <p class="login-tips">Tips : 请填写用户名密码。</p> -->
          <el-form-item style="float: right">
            <el-button type="text" v-show="codes" @click="runCode"
              >使用验证码登录</el-button
            >
            <el-button type="text" v-show="!codes" @click="invitation"
              >使用密码登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="conn_login">
        <img
          class="login-landing-img"
          src="../assets/img/login-landing.svg"
          alt=""
        />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      smsCountdown: 0,
      param: {
        username: "15717211494",
        password: "",
        codebase: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        codebase: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
      codes: true,
      show: true,
      count: "",
      timer: null,
      decode: false,
    };
  },
  created() {
    this.$store.commit("clearTags");
  },
  methods: {
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          this.$axios
            .post(`api/login`, {
              code: "",
              phone: this.param.username,
              password: this.param.password,
            })
            .then((res) => {
              console.log(res);
              let code = res.data.code;
              if (code === 401 || code === 404) {
                this.$message.error("登录失败,请确认输入是否有误");
              } else {
                let user = res.data.data.user.name;
                let usertoken = res.data.data.token;
                sessionStorage.setItem("ms_username", user);
                sessionStorage.setItem("token", usertoken);
                this.$message.success(`欢迎你! ${user}`);
                this.$router.push("/"); //进入首页
              }
            });
          // this.$message.success("登录成功");
          // sessionStorage.setItem("ms_username", this.param.username);
          // this.$router.push("/");
        } else if (this.codes == true) {
          this.$message.error("请输入账号和密码");
          return false;
        } else if (this.codes == false) {
          this.$message.error("请输入验证码");
          return false;
        }
      });
    },
    runCode() {
      this.codes = false;
    },
    invitation() {
      this.codes = true;
    },
    getCode() {
      const TIME_COUNT = 10;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 1 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
  },
};
</script>

<style scoped>
.login-bg {
  width: 100%;
  height: 100%;
  background: #3e3e3e;
}
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /* background-image: url(../assets/img/login-bg.jpg); */
  background-size: 100%;
  background-image: linear-gradient(-225deg, #8ad6ee, #50a7c2);
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  border-bottom: 1px solid #ddd;
  font-weight: 100;
  font-size: 2em;
  color: #50a7c2;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #50a7c2;
}
.login_rsp {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  margin: auto;
  height: 80vh;
  width: 1000px;
  border-radius: 30px;
  overflow: hidden;
  box-shadow: 0 1px 35px 0 rgb(0 0 0 / 30%);
  background-color: #fff;
}
.login_work {
  position: relative;
  width: 380px;
  height: 100%;
  float: right;
  background: #fff;
  box-sizing: border-box;
  padding: 40px;
  text-align: left;
}
.conn_login {
  /* width: 100%; */
  height: 100%;
  background: #e6fcfb;
}
.login-landing-img {
  height: 50%;
  display: inline-block;
  text-align: center;
  transform: translateY(-50%);
  top: 50%;
  left: 10%;
  position: relative;
}
</style>